FedData is a package for downloading data from federated
(usually US Federal) data sets, using a consistent, spatial-first API.
In this vignette, you’ll learn how to load the FedData
package, download data for an “template” area of interest, and make
simple web maps using the mapview
package.
FedData and define a study area
# Load FedData and magrittr
library(FedData)
library(magrittr)
library(terra)
# Install mapview if necessary
if (!require("mapview")) {
install.packages("mapview")
}
library(mapview)
mapviewOptions(
basemaps = c(),
homebutton = FALSE,
query.position = "topright",
query.digits = 2,
query.prefix = "",
legend.pos = "bottomright"
)
# Create a nice mapview template
plot_map <-
function(x, ...) {
if (inherits(x, "SpatRaster")) {
x %<>%
as("Raster")
}
bounds <-
FedData::meve %>%
sf::st_bbox() %>%
as.list()
mapview::mapview(x, ...)@map %>%
leaflet::removeLayersControl() %>%
leaflet::addTiles(
urlTemplate = "https://basemap.nationalmap.gov/ArcGIS/rest/services/USGSShadedReliefOnly/MapServer/tile/{z}/{y}/{x}"
) %>%
leaflet::addProviderTiles("Stamen.TonerLines") %>%
leaflet::addProviderTiles("Stamen.TonerLabels") %>%
leaflet::addPolygons(
data = FedData::meve,
color = "black",
fill = FALSE,
options = list(pointerEvents = "none"),
highlightOptions = list(sendToBack = TRUE)
) %>%
leaflet::fitBounds(
lng1 = bounds$xmin,
lng2 = bounds$xmax,
lat1 = bounds$ymin,
lat2 = bounds$ymax
)
}
# FedData comes loaded with the boundary of Mesa Verde National Park, for testing
FedData::meve
#> Geometry set for 1 feature
#> Geometry type: POLYGON
#> Dimension: XY
#> Bounding box: xmin: -108.5547 ymin: 37.15642 xmax: -108.3396 ymax: 37.35052
#> Geodetic CRS: WGS 84
plot_map(FedData::meve,
legend = FALSE
)
# Get the DAYMET (North America only)
# Returns a raster
DAYMET <- get_daymet(
template = FedData::meve,
label = "meve",
elements = c("prcp", "tmax"),
years = 1980:1985
)
plot_map(DAYMET$tmax$`1985-10-23`,
layer.name = "TMAX on 23 Oct 1985 (ºC)",
maxpixels = terra::ncell(DAYMET$tmax)
)
# Get the daily GHCN data (GLOBAL)
# Returns a list: the first element is the spatial locations of stations,
# and the second is a list of the stations and their daily data
GHCN.prcp <- get_ghcn_daily(
template = FedData::meve,
label = "meve",
elements = c("prcp")
)
#> Warning: attribute variables are assumed to be spatially constant throughout
#> all geometries
#> Warning in CPL_write_ogr(obj, dsn, layer, driver,
#> as.character(dataset_options), : GDAL Error 1:
#> /private/var/folders/ys/7l0z3wlx7z14qxn9v0m9ckhw0000gq/T/Rtmpks2YVl/FedData/extractions/ghcn/meve/meve_GHCN_stations.shp
#> does not appear to be a file or directory.
plot_map(GHCN.prcp$spatial,
label = GHCN.prcp$spatial %$%
paste0(ID, ": ", NAME),
legend = FALSE
)
# Elements for which you require the same data
# (i.e., minimum and maximum temperature for the same days)
# can be standardized using standardize==T
GHCN.temp <- get_ghcn_daily(
template = FedData::meve,
label = "meve",
elements = c("tmin", "tmax"),
years = 1980:1985,
standardize = TRUE
)
plot_map(GHCN.temp$spatial,
label = GHCN.temp$spatial %$%
paste0(ID, ": ", NAME),
legend = FALSE
)
# Get the NHD (USA ONLY)
NHD <-
get_nhd(
template = FedData::meve,
label = "meve"
)
# FedData comes with a simple plotting function for NHD data
plot_nhd(NHD, template = FedData::meve)
# Get the NRCS SSURGO data (USA ONLY)
SSURGO.MEVE <- get_ssurgo(
template = FedData::meve,
label = "meve"
)
SSURGO.MEVE$spatial %>%
dplyr::left_join(
SSURGO.MEVE$tabular$mapunit %>%
dplyr::mutate(mukey = as.character(mukey)),
by = c("MUKEY" = "mukey")
) %>%
dplyr::mutate(label = paste0(MUKEY, ": ", muname)) %>%
plot_map(
zcol = "label",
legend = FALSE
)
# Or, download by Soil Survey Area names
SSURGO.areas <- get_ssurgo(
template = c("CO670"),
label = "CO670"
)
SSURGO.areas$spatial %>%
dplyr::left_join(
SSURGO.areas$tabular$muaggatt %>%
dplyr::mutate(mukey = as.character(mukey)),
by = c("MUKEY" = "mukey")
) %>%
dplyr::mutate(label = paste0(MUKEY, ": ", muname)) %>%
plot_map(
zcol = "brockdepmin",
layer.name = "Minimum Bedrock Depth (cm)",
label = "label"
)